<template>
  <el-dialog title="分配角色" :visible="showrole" @close="outrole">
    <el-checkbox-group v-model="rolelist">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="outrole">取 消</el-button>
      <el-button type="primary" @click="setrole">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getrolelist } from '@/api/setting'
import { getUserDetailById, setUserRole } from '@/api/user'
export default {
  props: {
    showrole: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      rolelist: [],
      roleList: [],
      id: ''
    }
  },
  async created() {
    const { rows } = (await getrolelist()).data
    this.roleList = rows
  },
  methods: {
    outrole() {
      this.rolelist = []
      this.$emit('update:showrole', false)
    },
    async  getuserRole(id) {
      const { roleIds } = (await getUserDetailById(id)).data
      this.id = id
      this.rolelist = roleIds
    },
    async setrole() {
      await setUserRole({ roleIds: this.rolelist, id: this.id })
      this.$message.success('操作成功')
      this.outrole()
    }
  }
}
</script>

<style>

</style>
